//
//  AIUserDataManager.m
//  MiaMind
//
//  Created by Vill on 2023/11/24.
//

#import "AIUserDataManager.h"
#import "UIApplication+Extension.h"

static NSString *appendPath = @"userInfo_Path";

@implementation AIUserDataManager

/// 保存本地数据
/// @param key 存取key
/// @param obj 保存对象
/// @param type 存储类型
+ (void)saveLoacalDataWithKey:(NSString *)key
                   object:(id)obj
                    catheType:(LocalDataToolsSaveType)type{
    NSString *path;
    if (type == LocalDataToolsSaveType_Document) {
        path = [[UIApplication sharedApplication].documentsPath stringByAppendingPathComponent:appendPath];
    }else if (type == LocalDataToolsSaveType_Library){
        path = [[UIApplication sharedApplication].libraryPath stringByAppendingPathComponent:appendPath];
    }else{
        path = [[UIApplication sharedApplication].tempPath stringByAppendingPathComponent:appendPath];
    }
    path = [path stringByAppendingPathComponent:key];
    if (obj != nil) {
         [NSKeyedArchiver archiveRootObject:obj toFile:path];
    }
}

/// 加载本地数据
/// @param key 存取key
/// @param type 存储类型
+ (id)loadLocalWithKey:(NSString *)key
            catcheType:(LocalDataToolsSaveType)type{
    NSString *path;
    NSString *foler;
    if (type == LocalDataToolsSaveType_Document) {
        foler = [[UIApplication sharedApplication].documentsPath stringByAppendingPathComponent:appendPath];
        path = [foler stringByAppendingPathComponent:key];
    } else if (type == LocalDataToolsSaveType_Library) {
       foler = [[UIApplication sharedApplication].libraryPath stringByAppendingPathComponent:appendPath];
        path = [foler stringByAppendingPathComponent:key];
    } else {
        foler = [[UIApplication sharedApplication].tempPath stringByAppendingPathComponent:appendPath];
        path = [foler stringByAppendingPathComponent:key];
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir;
    if ([fileManager fileExistsAtPath:foler isDirectory:&isDir] && isDir) {

    } else {
        [fileManager createDirectoryAtPath:foler withIntermediateDirectories:YES attributes:nil error:nil];
    }
    id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return obj;
}

/// 删除本地存储数据
/// @param key 存取key
/// @param type 存储类型
+ (BOOL)deleteLoacalWithKey:(NSString *)key
                  catheType:(LocalDataToolsSaveType)type{
    NSString *path;
    if (type == LocalDataToolsSaveType_Document) {
        path = [NSString stringWithFormat:@"%@/%@", [[UIApplication sharedApplication].documentsPath stringByAppendingPathComponent:appendPath], key];
    } else if (type == LocalDataToolsSaveType_Library) {
        path = [NSString stringWithFormat:@"%@/%@", [[UIApplication sharedApplication].libraryPath stringByAppendingPathComponent:appendPath], key];
    } else {
         path = [NSString stringWithFormat:@"%@/%@", [[UIApplication sharedApplication].tempPath stringByAppendingPathComponent:appendPath], key];
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path] && [fileManager isDeletableFileAtPath:path]) {
        NSError *error = nil;
        [fileManager removeItemAtPath:path error:&error];
        if (error) {
            return NO;
        }
        return YES;
    } else {
        return NO;
    }
}

/// 删除某一存储类型的所有数据
/// @param type 存储类型
+ (BOOL)deleteAllLoacalWithType:(LocalDataToolsSaveType)type{
    NSString *path;
    if (type == LocalDataToolsSaveType_Document) {
        path = [[UIApplication sharedApplication].documentsPath stringByAppendingPathComponent:appendPath];
    } else if (type == LocalDataToolsSaveType_Library) {
        path = [[UIApplication sharedApplication].libraryPath stringByAppendingPathComponent:appendPath];
    } else {
         path = [[UIApplication sharedApplication].tempPath stringByAppendingPathComponent:appendPath];
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir;
    if ([fileManager fileExistsAtPath:path isDirectory:&isDir] && isDir) {
        NSError *error = nil;
               [fileManager removeItemAtPath:path error:&error];
               if (error) {
                   return NO;
               }
               return YES;
    }else {
        return NO;
    }
}

@end
